import os


def full_mark_name(work_path: str, file_name: str):
    return os.path.join(work_path, 'mark', file_name)


def full_mark_reply_name(work_path: str, file_name: str, reply_file_name: str):
    return os.path.join(work_path, 'mark', 'reply', file_name.split('.')[0], reply_file_name)

def full_name(work_path: str, file_name: str):
    return os.path.join(work_path, file_name)


def full_reply_name(work_path: str, file_name: str, reply_file_name: str):
    return os.path.join(work_path, 'reply', file_name.split('.')[0], reply_file_name)

def percent(per:int, all:int):
    if all == 0:
        return '100.0%'
    else:
        return '%.1f%%'%((per/all)*100)

def choose():
    print("""
    选择功能：
        1 检查（检查并显示完成百分比）并输出为文件

        4 退出
        
    > """)

    while True:
        a = input()
        if a.isalnum() is False:
            print("错误，请重试")
            continue
        else:
            a = int(a)
            if 1 <= a <= 4:  # ??! 令人生草的操作
                return a
            else:
                print("错误，请重试")
                continue


def get_num():
    while True:
        a = input()
        if a.isalnum() is False:
            print('错误，请重试')
            continue
        else:
            return int(a)


def get_a_wb_list(work_path, weibo_file_name: str):
    is_finish = os.path.isfile(full_mark_name(work_path=work_path, file_name=weibo_file_name))
    file_no_finsh_list = []
    file_list = []

    with open(full_reply_name(work_path=work_path, file_name=weibo_file_name, reply_file_name='reply_index.txt'),
              'r', encoding='utf-8') as f:
        l = f.readlines()
        for i in range(len(l)):
            file_list.append(l[i].replace('\n', ''))

    id2 = 0
    for reply_name in file_list:
        if os.path.isfile(full_mark_reply_name(work_path=work_path, file_name=weibo_file_name,
                                               reply_file_name=reply_name)) is False:
            file_no_finsh_list.append("%s %s"%(id2, reply_name))
        id2 += 1
    return [is_finish, file_list, file_no_finsh_list]


def all_chk(work_path: str):
    while True:
        a = input('输入输出文件夹路径').replace('"', '')
        if os.path.isdir(a) is True:
            print('是文件夹')
            break
        else:
            print("不是文件夹， 请重试")
            continue


    with open(os.path.join(work_path, 'index.txt'), 'r', encoding='utf-8') as f:
        l = f.readlines()
        for i in range(len(l)):
            l[i] = l[i].replace('\n', '')

    out_str_1 = '总百分比:\n'  # 总百分比
    out_str_2 = '微博层面的百分比\n'  # 微博层面的百分比
    out_str_3 = '未完成文件列表\n'  # 未完成文件列表

    all_num = len(l)
    no_finish = 0

    id=0
    for name in l:
        res = get_a_wb_list(work_path=work_path, weibo_file_name=name)
        print("%d %s %d"%(id,name, res[0]))
        out_str_2+="%d %s "%(id,name)
        if res[0] is False:
            no_finish += 1
            out_str_2 += '<没有标记>'
        else:
            out_str_2 += '<已标记>  '

        out_str_2 +=' %d/%d \t%s\n'%(len(res[1])-len(res[2]), len(res[1]), percent(len(res[1])-len(res[2]), len(res[1])))

        out_str_3+='%d %s:\n'%(id, name)
        id2 = 0
        for name2 in res[2]:
            # out_str_3 += "\t %d %s\n"%(id2, name2)
            out_str_3 += "\t %s\n"%(name2)
            id2 += 1
        id += 1
    out_str_1 += '%d/%d %s\n'%(all_num-no_finish, all_num, percent(all_num-no_finish, all_num))


    with open(
        os.path.join(a, 'my_out_put_list.txt'),
        'w+',
        encoding='utf-8'
    ) as f:
        f.write(out_str_1)
        f.write(out_str_2)
        f.write(out_str_3)



def main():
    while True:
        print("检查工具V1.0")
        work_path = input("选择工作目录：").replace('"', '')
        if (
                (os.path.exists(work_path) is True) and
                (os.path.isfile(work_path) is False) and
                (os.path.isfile(os.path.join(work_path, "index.txt")) is True)
        ):
            print("路径正确. 是否现在开始？[y/n]")
            a = input()
            if a == 'y' or a == 'Y':

                # 标注文件夹的建立，如果没有，就建立
                if os.path.exists(os.path.join(work_path, 'mark')) is False:
                    os.mkdir(os.path.join(work_path, 'mark'))
                #####检查开始
                cho = choose()
                if cho == 1:
                    all_chk(work_path=work_path)
                elif cho == 2:
                    print('暂不支持此功能！')
                elif cho == 3:
                    print('暂不支持此功能！')
                elif cho == 4:
                    print('现在退出')
                    return

                #####检查结束


if __name__ == '__main__':
    main()
